﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using MahApps.Metro.Controls;
using System.Data;
using System.Threading;


namespace MauThuTabControl
{
    /// <summary>
    /// Interaction logic for ThuTapControl1.xaml
    /// </summary>
    public partial class ThuTapControl1 : MetroWindow
    {
        ConnectionDB cn = new ConnectionDB();
       
        public ThuTapControl1()
        {
            InitializeComponent();
            LoadGW();
           
        }
        public void LoadGW()
        {
            DataTable dt = cn.LoadData("SELECT    dbo.QLNhapHangCT.maSanPham, dbo.QLNhapHangCT.thanhTien,  dbo.QLNhapHangCT.soPhieuN, dbo.QLNhapHangCT.soLuong, dbo.QLNhapHangCT.donGia, dbo.DMSanPham.tenSanPham FROM dbo.DMSanPham INNER JOIN dbo.QLNhapHangCT ON dbo.DMSanPham.maSanPham = dbo.QLNhapHangCT.maSanPham");
            dataGrid1.ItemsSource = dt.DefaultView;

        }
       
        private void _NUTongTien_GotFocus(object sender, RoutedEventArgs e)
        {
            _NUTongTien.HideUpDownButtons = false;
            _p1.IsEnabled = true;
            Thread.Sleep(2000);
            _p1.IsEnabled = false;
               
        }

        private void _NUTongTien_LostFocus(object sender, RoutedEventArgs e)
        {
            _NUTongTien.HideUpDownButtons = true;
            
            
        }

        private void dataGrid1_CellEditEnding(object sender, DataGridCellEditEndingEventArgs e)
        {
         
            
        }

        private void dataGrid1_RowEditEnding(object sender, DataGridRowEditEndingEventArgs e)
        {
            decimal _SoLuong = Convert.ToDecimal(((DataRowView)dataGrid1.SelectedItem).Row["soLuong"].ToString());
            string _SOP = ((DataRowView)dataGrid1.SelectedItem).Row["soPhieuN"].ToString();
            string _ma = ((DataRowView)dataGrid1.SelectedItem).Row["tenSanPham"].ToString();

            //cap nhat gt vao database
            string _Queryupdate = "update QLNhapHangCT set soLuong='" + _SoLuong + "' " +
                "where soPhieuN=N'" + _SOP + "' and maSanPham=N'" + _ma + "'";
            cn._ThucThiCauTruyVan(_Queryupdate);
            LoadGW();
        }

       
     
    }
}
